home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / SOURCES / WDASH.C < prev    next >
C/C++ Source or Header  |  1992-03-16  |  8KB  |  391 lines

  1. #include <InterViews\X11\palette.h>
  2. #include <Interviews\X11\dash.h>
  3. #include <math.h>
  4.  
  5. const int Bit_1 = 0x0001;
  6. const double Pix2 = 6.283185;
  7.  
  8. int GetSteps (int r) {
  9.     if (r < 20) {
  10.     return r;
  11.     } else {
  12.     return ((r*3) >> 2);
  13.     }
  14. }
  15.  
  16. Dash::Dash () {
  17.     pattern = nil;
  18.     count = 0;
  19. }
  20.  
  21. Dash::~Dash () {
  22. }
  23.  
  24. void Dash::SetPattern (Brush* b) {
  25.     pattern = b->rep->info;
  26.     count = b->rep->count;
  27. }
  28.  
  29. void Dash::_MoveTo (Coord x, Coord y) {
  30.     x0 = x; y0 = y;
  31. }
  32.  
  33. void Dash::Init (Canvas* c, PainterRep* r) {
  34.     canvas = c;
  35.     rep = r;
  36.     if (rep->foreground != nil) {
  37.     foreground = PALETTEINDEX(rep->foreground->PixelValue());
  38.     } else {
  39.     foreground = RGB(0, 0, 0);
  40.     }
  41.     if (rep->background != nil) {
  42.     background = PALETTEINDEX(rep->background->PixelValue());
  43.     } else {
  44.     background = RGB(255, 255, 255);
  45.     }
  46.     set = pattern[0];
  47.     index = 0;
  48. }
  49.  
  50. void Dash::_Init() {
  51. }
  52.  
  53. void Dash::_Finish () {
  54. }
  55.  
  56. void Dash::PutPixel () {
  57. }
  58.  
  59. void Dash::_LineTo(Coord x1, Coord y1) {
  60.     int  c1, c2, error;
  61.     int  m, n, dx, dy;
  62.  
  63.     if ((x0 == x1) && (y0 == y1)) {
  64.     PutPixel();
  65.     return;
  66.     }
  67.     x = x0; y = y0;
  68.     dx = x1 - x0;
  69.     dy = y1 - y0;
  70.     if (dx >= 0) {
  71.     m = 1;
  72.     } else {
  73.     m = -1;
  74.     dx = -dx;
  75.     }
  76.     if (dy >= 0) {
  77.     n = 1;
  78.     } else {
  79.     n = -1;
  80.     dy = -dy;
  81.     }
  82.     if (dx > dy) {
  83.     error = (dy << 1) - dx;
  84.     c1 = dy << 1;
  85.     c2 = (dy << 1) - (dx << 1);
  86.     } else {
  87.     error = (dx << 1) - dy;
  88.     c1 = dx << 1;
  89.     c2 = (dx << 1) - (dy << 1);
  90.     }
  91.  
  92.     do {
  93.     PutPixel();
  94.     if (dx > dy) {
  95.          x += m;
  96.     } else {
  97.         y += n;
  98.     }
  99.     if (error < 0) {
  100.         error += c1;
  101.     } else {
  102.         if (dx > dy) {
  103.         y += n;
  104.         } else {
  105.         x += m;
  106.         }
  107.         error += c2;
  108.     }
  109.     } while (!(x == x1 && y == y1));
  110.     PutPixel();
  111.     _MoveTo(x1, y1);
  112. }
  113.  
  114. void Dash::_Line(Coord x1, Coord y1, Coord x2, Coord y2) {
  115.     _Init();
  116.     _MoveTo(x1, y1);
  117.     _LineTo(x2, y2);
  118.     _Finish();
  119. }
  120.  
  121. void Dash::_Circle (Coord mx, Coord my, int r) {
  122.     double x, y, x0, y0;
  123.     double p, c1, c2;
  124.  
  125.     _Init();
  126.     int n = GetSteps(r);
  127.     p = Pix2/(n-1);
  128.     c1 = cos(p); c2 = sin(p);
  129.     x0 = mx + r; y0 = my;
  130.     _MoveTo(x0, y0);
  131.     for (int i = 1; i < n; i++) {
  132.     x = mx + (x0-mx)*c1 - (y0-my)*c2;
  133.     y = my + (x0-mx)*c2 + (y0-my)*c1;
  134.     _LineTo(x, y);
  135.     x0 = x; y0 = y;
  136.     }
  137.     _Finish();
  138. }
  139.  
  140. void Dash::_Ellipse (Coord mx, Coord my, int a, int b) {
  141.     double p, c1, c2, c3, s1, s2, s3;
  142.     int x0, y0, x, y, x1, y1;
  143.     double temp;
  144.  
  145.     _Init();
  146.     int n = GetSteps(a);
  147.     p = Pix2/(n-1);
  148.     c1 = 1; s1 = 0;
  149.     c2 = cos(p); s2 = sin(p);
  150.     c3 = 1; s3 = 0;
  151.     _MoveTo(mx + a*c3*c1 - b*s3*s1, my + a*c3*s1 + b*s3*s2);
  152.     for (int i = 0; i < n; i++) {
  153.     x1 = a*c3; y1 = b*s3;
  154.     x = mx + x1*c1 - y1*s1;
  155.     y = my + x1*s1 + y1*c1;
  156.     _LineTo(x, y);
  157.     temp = c3*c2 - s3*s2;
  158.     s3 = s3*c2 + c3*s2; c3 = temp;
  159.     }
  160.     _Finish();
  161. }
  162.  
  163. void Dash::_Rect (Coord left, Coord bottom, Coord right, Coord top) {
  164.     _Init();
  165.     _MoveTo(left, bottom);
  166.     _LineTo(left, top);
  167.     _LineTo(right, top);
  168.     _LineTo(right, bottom);
  169.     _LineTo(left, bottom);
  170.     _Finish();
  171. }
  172.  
  173. void Dash::_Polygon (Coord x[], Coord y[], int count) {
  174.     _Init();
  175.     _MoveTo(x[0], y[0]);
  176.     for (int i = 1; i < count; i++) {
  177.     _LineTo(x[i], y[i]);
  178.     }
  179.     _LineTo(x[0], y[0]);
  180.     _Finish();
  181. }
  182.  
  183. void Dash::_PolyLine (Coord x[], Coord y[], int count) {
  184.     _Init();
  185.     _MoveTo(x[0], y[0]);
  186.     for (int i = 1; i < count; i++) {
  187.     _LineTo(x[i], y[i]);
  188.     }
  189.     _Finish();
  190. }
  191.  
  192. void Dash::Point (Coord xpos, Coord ypos) {
  193.     x = xpos;
  194.     y = ypos;
  195.     _Init();
  196.     PutPixel();
  197.     _Finish();
  198. }
  199.  
  200. void Dash::Line(Coord x1, Coord y1, Coord x2, Coord y2) {
  201.     _Line(x1, y1, x2, y2);
  202. }
  203.  
  204. void Dash::Circle (Coord mx, Coord my, int r) {
  205.     _Circle(mx, my, r);
  206. }
  207.  
  208. void Dash::Ellipse (Coord mx, Coord my, int a, int b) {
  209.     _Ellipse(mx, my, a, b);
  210. }
  211.  
  212. void Dash::Rect (Coord left, Coord bottom, Coord right, Coord top) {
  213.     _Rect(left, bottom, right, top);
  214. }
  215.  
  216. void Dash::Polygon (Coord x[], Coord y[], int c) {
  217.     _Polygon(x, y, c);
  218. }
  219.  
  220. void Dash::PolyLine (Coord x[], Coord y[], int c) {
  221.     _PolyLine(x, y, c);
  222. }
  223.  
  224. /*******************************************************************/
  225.  
  226. void PixelDash::_Init () {
  227.     hDC = GetDC((HWND)canvas->Id());
  228.     SelectPalette(hDC, _palette->GetPalette(), 0);
  229.     RealizePalette(hDC);
  230.     if (rep->clip) {
  231.     holdRgn = SelectObject(hDC, rep->hRgn);
  232.     }
  233. }
  234.  
  235. void PixelDash::_Finish () {
  236.     if (rep->clip) {
  237.     SelectObject(hDC, holdRgn);
  238.     }
  239.     ReleaseDC((HWND)canvas->Id(), hDC);
  240. }
  241.  
  242. void PixelDash::PutPixel() {
  243.     if (set < 0) {
  244.         set++;
  245.         if (rep->fillbg) {
  246.         SetPixel(hDC, x, y, background);
  247.         }
  248.         return;
  249.     }
  250.     if (set == 0) {
  251.         set = pattern[++index%count];
  252.         set = (index & Bit_1) ? -set : set;
  253.         PutPixel();
  254.         return;
  255.     }
  256.     SetPixel(hDC, x, y, foreground);
  257.     set--;
  258. }
  259.  
  260. /*******************************************************************/
  261.  
  262. BitmapDash::BitmapDash (int w) {
  263.     width = w;
  264. }
  265.  
  266. void BitmapDash::_Init () {
  267.     hDC = GetDC((HWND)canvas->Id());
  268.     SelectPalette(hDC, _palette->GetPalette(), 0);
  269.     RealizePalette(hDC);
  270.  
  271.     hBitmap = CreateBitmap(width+1, width+1, 1, 1, NULL);
  272.     hMemDC  = CreateCompatibleDC(hDC);
  273.     holdBitmap = SelectObject(hMemDC, hBitmap);
  274.     PatBlt(hMemDC, 0, 0, width, width, BLACKNESS);
  275.     holdBrush1 = SelectObject(hMemDC, GetStockObject(WHITE_BRUSH));
  276.     SetTextColor(hMemDC, RGB(255, 255, 255));
  277.     ::Ellipse(hMemDC, -1, -1, width+1, width+1);
  278.  
  279.     hBrush = CreateSolidBrush(foreground);
  280.     holdBrush = SelectObject(hDC, hBrush);
  281.     if (rep->clip) {
  282.     holdRgn = SelectObject(hDC, rep->hRgn);
  283.     }
  284.  
  285.     offset = width >> 1;
  286.     offset = (offset < 1) ? 0 : offset;
  287. }
  288.  
  289. void BitmapDash::_Finish () {
  290.     SelectObject(hMemDC, holdBitmap);
  291.     SelectObject(hMemDC, holdBrush1);
  292.     DeleteDC(hMemDC);
  293.     DeleteObject(hBitmap);
  294.     if (rep->clip) {
  295.     SelectObject(hDC, holdRgn);
  296.     }
  297.     SelectObject(hDC, holdBrush);
  298.     DeleteObject(hBrush);
  299.     ReleaseDC((HWND)canvas->Id(), hDC);
  300. }
  301.  
  302. void BitmapDash::PutPixel() {
  303.     if (set < 0) {
  304.         set++;
  305.         return;
  306.     }
  307.     if (set == 0) {
  308.         set = pattern[++index%count];
  309.         set = (index & Bit_1) ? -set : set;
  310.         PutPixel();
  311.         return;
  312.     }
  313.     BitBlt(
  314.     hDC, x - offset, y - offset, width, width, hMemDC, 0, 0, 0xE20746L
  315.     );
  316.     set--;
  317. }
  318.  
  319. void BitmapDash::SaveSettings () {
  320.     for (int i = 0; i < count; i++) {
  321.     pattern[i] = -pattern[i];
  322.     }
  323.     save_foreground = foreground;
  324.     foreground = background;
  325. }
  326.  
  327. void BitmapDash::RestoreSettings () {
  328.     for (int i = 0; i < count; i++) {
  329.     pattern[i] = -pattern[i];
  330.     }
  331.     foreground = save_foreground;
  332.     set = pattern[0];
  333.     index = 0;
  334. }
  335.  
  336. void BitmapDash::Line(Coord x1, Coord y1, Coord x2, Coord y2) {
  337.     if (rep->fillbg) {
  338.     SaveSettings();
  339.     _Line(x1, y1, x2, y2);
  340.     RestoreSettings();
  341.     }
  342.     _Line(x1, y1, x2, y2);
  343. }
  344.  
  345. void BitmapDash::Circle (Coord mx, Coord my, int r) {
  346.     if (rep->fillbg) {
  347.     SaveSettings();
  348.     _Circle(mx, my, r);
  349.     RestoreSettings();
  350.     }
  351.     _Circle(mx, my, r);
  352. }
  353.  
  354. void BitmapDash::Ellipse (Coord mx, Coord my, int a, int b) {
  355.     if (rep->fillbg) {
  356.     SaveSettings();
  357.     _Ellipse(mx, my, a, b);
  358.     RestoreSettings();
  359.     }
  360.     _Ellipse(mx, my, a, b);
  361. }
  362.  
  363. void BitmapDash::Rect (Coord left, Coord bottom, Coord right, Coord top) {
  364.     if (rep->fillbg) {
  365.     SaveSettings();
  366.     _Rect(left, bottom, right, top);
  367.     RestoreSettings();
  368.     }
  369.     _Rect(left, bottom, right, top);
  370. }
  371.  
  372. void BitmapDash::Polygon (Coord x[], Coord y[], int c) {
  373.     if (rep->fillbg) {
  374.     SaveSettings();
  375.     _Polygon(x, y, c);
  376.     RestoreSettings();
  377.     }
  378.     _Polygon(x, y, c);
  379. }
  380.  
  381. void BitmapDash::PolyLine (Coord x[], Coord y[], int c) {
  382.     if (rep->fillbg) {
  383.     SaveSettings();
  384.     _PolyLine(x, y, c);
  385.     RestoreSettings();
  386.     }
  387.     _PolyLine(x, y, c);
  388. }
  389.  
  390.  
  391.